home *** CD-ROM | disk | FTP | other *** search
/ Aminet 49 / Aminet 49 (2002)(GTI - Schatztruhe)[!][Jun 2002].iso / Aminet / util / libs / ttrender.lha / ttrender-2.0 / Developer / source / ftsystem.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-06  |  16.3 KB  |  424 lines

  1. // TetiSoft: Modified to avoid fopen() fclose() fread() fseek() ftell()
  2. // malloc() realloc() and free() which can't be used in an amiga
  3. // shared run-time library linked with libinit.o
  4.  
  5. #include <exec/memory.h>
  6.  
  7. #ifdef __GNUC__
  8. // Avoid warnings "struct X declared inside parameter list"
  9. #include <exec/devices.h>
  10. #include <exec/io.h>
  11. #include <exec/semaphores.h>
  12. #include <dos/exall.h>
  13. #endif
  14.  
  15. // Necessary with OS3.9 includes
  16. #define __USE_SYSBASE
  17.  
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20.  
  21.  
  22. /***************************************************************************/
  23. /*                                                                         */
  24. /*  ftsystem.c                                                             */
  25. /*                                                                         */
  26. /*    ANSI-specific FreeType low-level system interface (body).            */
  27. /*                                                                         */
  28. /*  Copyright 1996-2001 by                                                 */
  29. /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
  30. /*                                                                         */
  31. /*  This file is part of the FreeType project, and may only be used,       */
  32. /*  modified, and distributed under the terms of the FreeType project      */
  33. /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
  34. /*  this file you indicate that you have read the license and              */
  35. /*  understand and accept it fully.                                        */
  36. /*                                                                         */
  37. /***************************************************************************/
  38.  
  39.   /*************************************************************************/
  40.   /*                                                                       */
  41.   /* This file contains the default interface used by FreeType to access   */
  42.   /* low-level, i.e. memory management, i/o access as well as thread       */
  43.   /* synchronisation.  It can be replaced by user-specific routines if     */
  44.   /* necessary.                                                            */
  45.   /*                                                                       */
  46.   /*************************************************************************/
  47.  
  48.  
  49. #include <ft2build.h>
  50. #include FT_CONFIG_CONFIG_H
  51. #include FT_INTERNAL_DEBUG_H
  52. #include FT_SYSTEM_H
  53. #include FT_ERRORS_H
  54. #include FT_TYPES_H
  55. #include FT_FREETYPE_H
  56.  
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60.  
  61. #include "lib.h"
  62. extern struct TTRenderBase *Ttb;
  63.  
  64. // TetiSoft: C implementation of AllocVecPooled (see autodoc exec/AllocPooled)
  65. APTR AllocVecPooled(APTR poolHeader, ULONG memSize)
  66. {
  67.         struct Library *SysBase = Ttb->ttb_SysBase;
  68.         ULONG newSize = memSize + sizeof(ULONG);
  69.  
  70.         ULONG *mem = AllocPooled(poolHeader, newSize);
  71.  
  72.         if (!mem)
  73.                 return NULL;
  74.         *mem = newSize;
  75.         return mem + 1;
  76. }
  77.  
  78. // TetiSoft: C implementation of FreeVecPooled (see autodoc exec/AllocPooled)
  79. void FreeVecPooled(APTR poolHeader, APTR memory)
  80. {
  81.         struct Library *SysBase = Ttb->ttb_SysBase;
  82.         ULONG *realmem = (ULONG *)memory - 1;
  83.  
  84.         FreePooled(poolHeader, realmem, *realmem);
  85. }
  86.  
  87.   /*************************************************************************/
  88.   /*                                                                       */
  89.   /*                       MEMORY MANAGEMENT INTERFACE                     */
  90.   /*                                                                       */
  91.   /*************************************************************************/
  92.  
  93.   /*************************************************************************/
  94.   /*                                                                       */
  95.   /* It is not necessary to do any error checking for the                  */
  96.   /* allocation-related functions.  This will be done by the higher level  */
  97.   /* routines like FT_Alloc() or FT_Realloc().                             */
  98.   /*                                                                       */
  99.   /*************************************************************************/
  100.  
  101.  
  102.   /*************************************************************************/
  103.   /*                                                                       */
  104.   /* <Function>                                                            */
  105.   /*    ft_alloc                                                           */
  106.   /*                                                                       */
  107.   /* <Description>                                                         */
  108.   /*    The memory allocation function.                                    */
  109.   /*                                                                       */
  110.   /* <Input>                                                               */
  111.   /*    memory :: A pointer to the memory object.                          */
  112.   /*                                                                       */
  113.   /*    size   :: The requested size in bytes.                             */
  114.   /*                                                                       */
  115.   /* <Return>                                                              */
  116.   /*    The address of newly allocated block.                              */
  117.   /*                                                                       */
  118.   FT_CALLBACK_DEF( void* )
  119.   ft_alloc( FT_Memory  memory,
  120.             long       size )
  121.   {
  122. //  FT_UNUSED( memory );
  123.  
  124. //  return malloc( size );
  125.     return AllocVecPooled( memory->user, size );
  126.   }
  127.  
  128.  
  129.   /*************************************************************************/
  130.   /*                                                                       */
  131.   /* <Function>                                                            */
  132.   /*    ft_realloc                                                         */
  133.   /*                                                                       */
  134.   /* <Description>                                                         */
  135.   /*    The memory reallocation function.                                  */
  136.   /*                                                                       */
  137.   /* <Input>                                                               */
  138.   /*    memory   :: A pointer to the memory object.                        */
  139.   /*                                                                       */
  140.   /*    cur_size :: The current size of the allocated memory block.        */
  141.   /*                                                                       */
  142.   /*    new_size :: The newly requested size in bytes.                     */
  143.   /*                                                                       */
  144.   /*    block    :: The current address of the block in memory.            */
  145.   /*                                                                       */
  146.   /* <Return>                                                              */
  147.   /*    The address of the reallocated memory block.                       */
  148.   /*                                                                       */
  149.   FT_CALLBACK_DEF( void* )
  150.   ft_realloc( FT_Memory  memory,
  151.               long       cur_size,
  152.               long       new_size,
  153.               void*      block )
  154.   {
  155. //  FT_UNUSED( memory );
  156. //  FT_UNUSED( cur_size );
  157.  
  158. //  return realloc( block, new_size );
  159.     struct Library *SysBase = Ttb->ttb_SysBase;
  160.  
  161.     void* new_block;
  162.  
  163.     new_block = AllocVecPooled ( memory->user, new_size );
  164.     if ( new_block != NULL )
  165.     {
  166.       CopyMem ( block, new_block,
  167.                 ( new_size > cur_size ) ? cur_size : new_size );
  168.       FreeVecPooled ( memory->user, block );
  169.     }
  170.     return new_block;
  171.   }
  172.  
  173.  
  174.   /*************************************************************************/
  175.   /*                                                                       */
  176.   /* <Function>                                                            */
  177.   /*    ft_free                                                            */
  178.   /*                                                                       */
  179.   /* <Description>                                                         */
  180.   /*    The memory release function.                                       */
  181.   /*                                                                       */
  182.   /* <Input>                                                               */
  183.   /*    memory  :: A pointer to the memory object.                         */
  184.   /*                                                                       */
  185.   /*    block   :: The address of block in memory to be freed.             */
  186.   /*                                                                       */
  187.   FT_CALLBACK_DEF( void )
  188.   ft_free( FT_Memory  memory,
  189.            void*      block )
  190.   {
  191. //  FT_UNUSED( memory );
  192.  
  193. //  free( block );
  194.  
  195.     FreeVecPooled( memory->user, block );
  196.   }
  197.  
  198.  
  199.   /*************************************************************************/
  200.   /*                                                                       */
  201.   /*                     RESOURCE MANAGEMENT INTERFACE                     */
  202.   /*                                                                       */
  203.   /*************************************************************************/
  204.  
  205.  
  206.   /*************************************************************************/
  207.   /*                                                                       */
  208.   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  209.   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  210.   /* messages during execution.                                            */
  211.   /*                                                                       */
  212. #undef  FT_COMPONENT
  213. #define FT_COMPONENT  trace_io
  214.  
  215.   /* We use the macro STREAM_FILE for convenience to extract the       */
  216.   /* system-specific stream handle from a given FreeType stream object */
  217. //#define STREAM_FILE( stream )  ( (FILE*)stream->descriptor.pointer )
  218. #define STREAM_FILE( stream )  ( (BPTR)stream->descriptor.pointer )     // TetiSoft
  219.  
  220.  
  221.   /*************************************************************************/
  222.   /*                                                                       */
  223.   /* <Function>                                                            */
  224.   /*    ft_close_stream                                                    */
  225.   /*                                                                       */
  226.   /* <Description>                                                         */
  227.   /*    The function to close a stream.                                    */
  228.   /*                                                                       */
  229.   /* <Input>                                                               */
  230.   /*    stream :: A pointer to the stream object.                          */
  231.   /*                                                                       */
  232.   FT_CALLBACK_DEF( void )
  233.   ft_close_stream( FT_Stream  stream )
  234.   {
  235.     struct Library *DOSBase = Ttb->ttb_DOSBase;
  236.  
  237. //  fclose( STREAM_FILE( stream ) );
  238.     Close( STREAM_FILE( stream ) );     // TetiSoft
  239.  
  240.     stream->descriptor.pointer = NULL;
  241.     stream->size               = 0;
  242.     stream->base               = 0;
  243.   }
  244.  
  245.  
  246.   /*************************************************************************/
  247.   /*                                                                       */
  248.   /* <Function>                                                            */
  249.   /*    ft_io_stream                                                       */
  250.   /*                                                                       */
  251.   /* <Description>                                                         */
  252.   /*    The function to open a stream.                                     */
  253.   /*                                                                       */
  254.   /* <Input>                                                               */
  255.   /*    stream :: A pointer to the stream object.                          */
  256.   /*                                                                       */
  257.   /*    offset :: The position in the data stream to start reading.        */
  258.   /*                                                                       */
  259.   /*    buffer :: The address of buffer to store the read data.            */
  260.   /*                                                                       */
  261.   /*    count  :: The number of bytes to read from the stream.             */
  262.   /*                                                                       */
  263.   /* <Return>                                                              */
  264.   /*    The number of bytes actually read.                                 */
  265.   /*                                                                       */
  266.   FT_CALLBACK_DEF( unsigned long )
  267.   ft_io_stream( FT_Stream       stream,
  268.                 unsigned long   offset,
  269.                 unsigned char*  buffer,
  270.                 unsigned long   count )
  271.   {
  272.     struct Library *DOSBase = Ttb->ttb_DOSBase;
  273. //  FILE*  file;
  274.     BPTR   file;        // TetiSoft
  275.  
  276.  
  277.     file = STREAM_FILE( stream );
  278.  
  279. //  fseek( file, offset, SEEK_SET );
  280.     Seek( file, offset, OFFSET_BEGINNING );     // TetiSoft
  281.  
  282. //  return (unsigned long)fread( buffer, 1, count, file );
  283.     return (unsigned long)FRead( file, buffer, 1, count);
  284.   }
  285.  
  286.  
  287.   /* documentation is in ftobjs.h */
  288.  
  289.   FT_EXPORT_DEF( FT_Error )
  290.   FT_New_Stream( const char*  filepathname,
  291.                  FT_Stream    astream )
  292.   {
  293.     struct Library *DOSBase = Ttb->ttb_DOSBase;
  294. //  FILE*  file;
  295.     BPTR   file;                // TetiSoft
  296.     struct FileInfoBlock *fib;  // TetiSoft
  297.  
  298.  
  299.     if ( !astream )
  300.       return FT_Err_Invalid_Stream_Handle;
  301.  
  302. //  file = fopen( filepathname, "rb" );
  303.     file = Open((STRPTR)filepathname, MODE_OLDFILE );  // TetiSoft
  304.     if ( !file )
  305.     {
  306.       FT_ERROR(( "FT_New_Stream:" ));
  307.       FT_ERROR(( " could not open `%s'\n", filepathname ));
  308.  
  309.       return FT_Err_Cannot_Open_Resource;
  310.     }
  311.  
  312. //  fseek( file, 0, SEEK_END );
  313. //  astream->size = ftell( file );
  314. //  fseek( file, 0, SEEK_SET );
  315.     fib = AllocDosObject( DOS_FIB, NULL );
  316.     if ( !fib )
  317.     {
  318.       Close ( file );
  319.       FT_ERROR(( "FT_New_Stream:" ));
  320.       FT_ERROR(( " could not open `%s'\n", filepathname ));
  321.  
  322.       return FT_Err_Cannot_Open_Resource;
  323.     }
  324.     if (!( ExamineFH ( file, fib )))
  325.     {
  326.       FreeDosObject(DOS_FIB, fib);
  327.       Close ( file );
  328.       FT_ERROR(( "FT_New_Stream:" ));
  329.       FT_ERROR(( " could not open `%s'\n", filepathname ));
  330.  
  331.       return FT_Err_Cannot_Open_Resource;
  332.     }
  333.     astream->size = fib->fib_Size;
  334.     FreeDosObject(DOS_FIB, fib);
  335.  
  336. //  astream->descriptor.pointer = file;
  337.     astream->descriptor.pointer = (void *)file;
  338.  
  339.     astream->pathname.pointer   = (char*)filepathname;
  340.     astream->pos                = 0;
  341.  
  342.     astream->read  = ft_io_stream;
  343.     astream->close = ft_close_stream;
  344.  
  345.     FT_TRACE1(( "FT_New_Stream:" ));
  346.     FT_TRACE1(( " opened `%s' (%d bytes) successfully\n",
  347.                 filepathname, astream->size ));
  348.  
  349.     return FT_Err_Ok;
  350.   }
  351.  
  352.  
  353.  
  354. #ifdef FT_DEBUG_MEMORY
  355.  
  356.   extern FT_Int
  357.   ft_mem_debug_init( FT_Memory  memory );
  358.   
  359.   extern void
  360.   ft_mem_debug_done( FT_Memory  memory );
  361.   
  362. #endif  
  363.       
  364.  
  365.   /* documentation is in ftobjs.h */
  366.  
  367.   FT_EXPORT_DEF( FT_Memory )
  368.   FT_New_Memory( void )
  369.   {
  370.     struct Library *SysBase = Ttb->ttb_SysBase;
  371.     FT_Memory  memory;
  372.  
  373.  
  374. //  memory = (FT_Memory)malloc( sizeof ( *memory ) );
  375.     memory = (FT_Memory)AllocVec( sizeof ( *memory ), MEMF_PUBLIC );
  376.     if ( memory )
  377.     {
  378. //    memory->user    = 0;
  379. #ifdef __GNUC__
  380.       memory->user    = CreatePool ( MEMF_PUBLIC, 2048, 2048 );
  381. #else
  382.       memory->user    = AsmCreatePool ( MEMF_PUBLIC, 2048, 2048, SysBase );
  383. #endif
  384.       if ( memory->user == NULL )
  385.       {
  386.         FreeVec ( memory );
  387.         memory = NULL;
  388.       }
  389.       else
  390.       {
  391.         memory->alloc   = ft_alloc;
  392.         memory->realloc = ft_realloc;
  393.         memory->free    = ft_free;
  394. #ifdef FT_DEBUG_MEMORY
  395.         ft_mem_debug_init( memory );
  396. #endif    
  397.       }
  398.     }
  399.  
  400.     return memory;
  401.   }
  402.  
  403.  
  404.   /* documentation is in ftobjs.h */
  405.  
  406.   FT_EXPORT_DEF( void )
  407.   FT_Done_Memory( FT_Memory  memory )
  408.   {
  409.     struct Library *SysBase = Ttb->ttb_SysBase;
  410. #ifdef FT_DEBUG_MEMORY
  411.     ft_mem_debug_done( memory );
  412. #endif  
  413.  
  414. #ifdef __GNUC__
  415.     DeletePool( memory->user );
  416. #else
  417.     AsmDeletePool( memory->user, SysBase );
  418. #endif
  419.     FreeVec( memory );
  420.   }
  421.  
  422.  
  423. /* END */
  424.